package com.at.activemq.schedule_message;


import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ScheduledMessage;
import org.junit.Test;

import javax.jms.*;

// 带延时投递的消息生产
public class JmsProduce {
    public static final String ACTIVEMQ_URL = "tcp://192.168.91.133:61616";
    public static final String QUEUE_NAME = "delay";

    public static void main(String[] args) throws Exception {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        Connection connection = activeMQConnectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue(QUEUE_NAME);
        MessageProducer messageProducer = session.createProducer(queue);
        messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);   // 持久化  如果开启
        long delay = 3 * 1000; //延迟投递的时间
        long period = 4 * 1000; //重复投递的时间间隔
        int repeat = 5;
        for (int i = 1; i < 4; i++) {
            TextMessage textMessage = session.createTextMessage("delay msg--" + i);
            // 消息每过 3 秒投递，每 4 秒重复投递一次 ，一共重复投递5次
            textMessage.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, delay);
            textMessage.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD, period);
            textMessage.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, repeat);
            messageProducer.send(textMessage);
        }
        // 9 关闭资源
        messageProducer.close();
        session.close();
        connection.close();
        // session.commit();
        System.out.println("  **** 消息发送到MQ完成 ****");
    }

    @Test
    public void testDelayProduce() throws JMSException {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        Connection connection = activeMQConnectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue(QUEUE_NAME);
        MessageProducer messageProducer = session.createProducer(queue);
        messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);   // 持久化  如果开启
        long delay = 3 * 1000; //延迟投递的时间
        long period = 4 * 1000; //重复投递的时间间隔
        int repeat = 5;
        for (int i = 1; i < 4; i++) {
            TextMessage textMessage = session.createTextMessage("delay msg--" + i);
            // 消息每过 3 秒投递，每 4 秒重复投递一次 ，一共重复投递5次
            textMessage.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, delay);
            textMessage.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD, period);
            textMessage.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, repeat);
            messageProducer.send(textMessage);
        }
        // 9 关闭资源
        messageProducer.close();
        session.close();
        connection.close();
        // session.commit();
        System.out.println("  **** 消息发送到MQ完成 ****");

    }
}
